package cn.kgc.mcartoon.service.impl;

import cn.kgc.mcartoon.dto.ActiveDto;
import cn.kgc.mcartoon.dto.CartoonDto;
import cn.kgc.mcartoon.dto.HotDto;
import cn.kgc.mcartoon.entity.MmCartoonClassificationDicDO;
import cn.kgc.mcartoon.entity.MmCartoonDO;
import cn.kgc.mcartoon.mapper.MmCartoonClassificationDicMapper;
import cn.kgc.mcartoon.mapper.MmCartoonMapper;
import cn.kgc.mcartoon.req.CartoonReq;
import cn.kgc.mcartoon.service.MmCartoonService;
import cn.kgc.mcartoon.utils.Constants;
import cn.kgc.mcartoon.utils.DateUtils;
import cn.kgc.mcartoon.utils.RedisClient;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 漫画表 服务实现类
 * </p>
 *
 * @author christopher
 * @since 2021-09-23
 */
@Service
public class MmCartoonServiceImpl extends ServiceImpl<MmCartoonMapper, MmCartoonDO> implements MmCartoonService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MmCartoonMapper cartoonMapper;

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private MmCartoonClassificationDicMapper cartoonClassificationDicMapper;

    @Override
    public List<CartoonDto> selectOriginalCartoonList(CartoonReq cartoonReq) {

        List<CartoonDto> cartoonDtos = cartoonMapper.selectOriginalCartoonList(cartoonReq);

        cartoonDtos = this.formatCartoon(cartoonDtos);
        return cartoonDtos;
    }

    @Override
    public List<HotDto> selectHotCartoonList(CartoonReq cartoonReq) {

        List<HotDto> hots = new ArrayList<>();

        //查询综合排行

        List<CartoonDto> allCartoonDtos = (List<CartoonDto>) redisClient.get("hot" + Constants.Home.AREAS);
        if(null != allCartoonDtos){
            hots = this.dtoHotCarton(Constants.Home.AREAS, allCartoonDtos,hots);
        }else {
            allCartoonDtos = cartoonMapper.selectOriginalCartoonList(cartoonReq);
            redisClient.set("hot" + Constants.Home.AREAS,allCartoonDtos);
            hots = this.dtoHotCarton(Constants.Home.AREAS, allCartoonDtos,hots);
        }

        //查询国内漫画排行
        List<CartoonDto> cnCarToonDtos  = (List<CartoonDto>) redisClient.get("hot" + Constants.Home.China);
        if(null != cnCarToonDtos){
            hots = this.dtoHotCarton(Constants.Home.China, cnCarToonDtos,hots);
        }else {
            //设置区域为中国
            cartoonReq.setAreaId(21L);
            cnCarToonDtos = cartoonMapper.selectOriginalCartoonList(cartoonReq);
            redisClient.set("hot" + Constants.Home.China,cnCarToonDtos);
            hots = this.dtoHotCarton(Constants.Home.China, cnCarToonDtos,hots);
        }


        List<CartoonDto> jpCarToonDtos  = (List<CartoonDto>) redisClient.get("hot" + Constants.Home.Japan);
        if (null != jpCarToonDtos){
            hots = this.dtoHotCarton(Constants.Home.Japan,jpCarToonDtos,hots);
        }
        else {
            cartoonReq.setAreaId(22L);
            //查询日漫漫画排行
            jpCarToonDtos = cartoonMapper.selectOriginalCartoonList(cartoonReq);
            redisClient.set("hot" + Constants.Home.Japan,jpCarToonDtos);
            hots = this.dtoHotCarton(Constants.Home.Japan,jpCarToonDtos,hots);
        }

        return hots;
    }

    @Override
    public List<ActiveDto> selectActiveCartoonList() {

        List<ActiveDto> activeDtoList = new ArrayList<>();
        //返回一个1-7的list
        List<String> dateList = DateUtils.getPastDates(Constants.Home.ACTIVES_DAYS);

        //遍历7天
        dateList.forEach(searchDate ->{
            List<CartoonDto> cartoonDtoList = null;
            //定义查询条件
            CartoonReq cartoonReq = new CartoonReq(Constants.Cartoon.Sort.ACTIVE, 0, Constants.Home.ACTIVES_SIZE);
            //设置查询每天
            cartoonReq.setLastCapterUpTime(searchDate);
            //根据条件查询漫画结合
            cartoonDtoList = cartoonMapper.selectOriginalCartoonList(cartoonReq);

            if (!cartoonDtoList.isEmpty()){
                //formatCartoonDtoList(cartoonDtoList);
                ActiveDto activeDto = new ActiveDto(searchDate, cartoonDtoList);
                activeDtoList.add(activeDto);
            }

        });

        return activeDtoList;


    }

    private List<HotDto> dtoHotCarton(int i, List<CartoonDto> cartoonDtos, List<HotDto> hots) {

        hots.add(new HotDto(i,cartoonDtos));
        return hots;
    }

    private List<CartoonDto>  formatCartoon(List<CartoonDto> cartoonDtos) {
        if(!cartoonDtos.isEmpty()){
            cartoonDtos.forEach(cartoonDto ->{
                finalFormatCartonm(cartoonDto);
            });
        }
        return cartoonDtos;

    }

    private CartoonDto finalFormatCartonm(CartoonDto cartoonDto) {

        List<String> classificationNames = null;

        classificationNames = (List<String>) redisClient.get("CartoonClassByCartoon" + cartoonDto.getCartoonId());
        if (null != classificationNames){
            cartoonDto.setThemes(classificationNames);
        }else {
            classificationNames = cartoonClassificationDicMapper.selectCartoonClassificationByCartoonId(cartoonDto.getCartoonId());
            redisClient.set("CartoonClassByCartoon" + cartoonDto.getCartoonId(),classificationNames);
        }


        return cartoonDto;

    }
}
